package com.mdd.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.common.entity.TimeSlot;
import com.mdd.common.mapper.TimeSlotMapper;
import com.mdd.common.service.ITimeService;
import com.mdd.common.util.ConfigUtils;
import com.mdd.common.util.DateUtils;
import com.mdd.common.util.StringUtils;
import com.mdd.common.vo.timeslot.TimeSlotListVo;
import com.mdd.common.vo.timeslot.TimeSlotVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class TimeServiceImpl implements ITimeService {

    @Resource
    private TimeSlotMapper timeSlotMapper;


    @Override
    public List<TimeSlot> getTimeList() {
        LambdaQueryWrapper<TimeSlot> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TimeSlot::getIsDelete, 0).eq(TimeSlot::getIsDisable, 0);
        wrapper.orderByDesc(TimeSlot::getSort);
        return timeSlotMapper.selectList(wrapper);
    }

    @Override
    public List<TimeSlotListVo> getConfTimeSlotList() {
        // 配置
        Map<String, String> website = ConfigUtils.get("website");
        String jiazOrderDay = website.getOrDefault("jiazOrderDay", "0");
        String jiazOrderHour = website.getOrDefault("jiazOrderHour", "0");

        Integer addNumTime = StringUtils.isNotBlank(jiazOrderHour) ? Integer.parseInt(jiazOrderHour) : 0;

        // 页面配置的时间
        List<TimeSlot> list = getTimeList();

        // 返回
        List<TimeSlotListVo> result = new ArrayList<>();

        Integer days = StringUtils.isNotBlank(jiazOrderDay) ? Integer.parseInt(jiazOrderDay) : 0;
        for (int d = 0; d < days; d++) {
            TimeSlotListVo vo = new TimeSlotListVo();
            // 日期
            //String weekName = DateUtils.dayOfWeek(d);
            String weekName = com.mdd.common.util.DateUtils.dayOfWeek(d);
            //String date = DateUtils.getMonthDay(d);
            String date = com.mdd.common.util.DateUtils.getMonthDay(d);

            vo.setWeekName(weekName);
            vo.setDate(date);

            // 时间
            List<TimeSlotVo> timeList = new ArrayList<>();

            for (TimeSlot item : list) {
                TimeSlotVo slotVo = new TimeSlotVo();
                BeanUtils.copyProperties(item, slotVo);
                slotVo.setIsSelected(false);
                String startTime = item.getStartTime();
                slotVo.setStartTime(StringUtils.isNotBlank(startTime) ? startTime.substring(0, 5) : "");
                // 判断今天
                if (d == 0) {
                    Long futureTime = com.mdd.common.util.DateUtils.getAddNumTime(addNumTime);

                    Long nowTime = com.mdd.common.util.DateUtils.getLongNowTime(slotVo.getStartTime());
                    if (nowTime >= futureTime) {
                        timeList.add(slotVo);
                    }
                } else {
                    timeList.add(slotVo);
                }
            }
            if (Objects.nonNull(timeList) && timeList.size() > 0) {
                vo.setTimeList(timeList);
                result.add(vo);
            }
        }
        return result;
    }
}
